import 'package:kq_flutter_core_widget/utils/ex/string_ex.dart';
import 'package:kq_flutter_core_widget/utils/search/search_able.dart';

/// 搜索帮助类
class SearchUtil {
  /// 根据搜索内容搜索出相关数据
  /// 用户需要搜索的数据类需要继承[SearchAble],并实现[toSearch]方法
  static List<T> search<T extends SearchAble>(String? searchText, List<T> data) {
    List<T> searchData = [];
    if (searchText.isNotNullOrEmpty) {
      for (T t in data) {
        if (t.toSearch().isNotNullOrEmpty &&
            t.toSearch().toLowerCase().trim().contains(searchText!.toLowerCase())) {
          searchData.add(t);
        }
      }
    }
    return searchData;
  }
}
